package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.common.QueryCpUtil;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.WmsDlPurInAcceptQueryCmd;
import com.jackrain.nea.fc.dl.mapper.WMSDLUpdateMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;


/**
 * 采购入库单验收， 收货方为WMS管控
 * @author kangcong
 * @date 2018/08/20
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class WmsDlPurInAcceptQueryCmdImpl extends CommandAdapter implements WmsDlPurInAcceptQueryCmd {

    @Reference(version = "2", group = "cp")
    private QueryCmd queryCmd;

    @Autowired
    QueryCpUtil postQueryEvemt;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        WMSDLUpdateMapper wmsDlUpdateMapper = ApplicationContextHandle.getBean(WMSDLUpdateMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("WmsDlPurInAcceptQueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        JSONArray dataArray = param.getJSONArray("IDS");
        JSONArray fixArray = new JSONArray();

        for (int i = 0; i < dataArray.size(); i++) {

            JSONObject fixcolumn = new JSONObject();

            String idStr = dataArray.getString(i);
            HashMap mainMap = wmsDlUpdateMapper.selectWmsPurIn(idStr);

            if (mainMap == null) {
                log.debug("调拨入库单验收WMS，数据为空，idStr="+idStr);
                continue;
            }
            //查询主表
            JSONObject mainJo = new JSONObject(mainMap);
            //跨库查询收货店仓是否是wms管控
            Long storeecode = mainJo.getLong("DEST_ECODE");
            if (null != storeecode) {
                JSONObject whereJo = new JSONObject();
                whereJo.put("ECODE", storeecode);
                JSONObject result = postQueryEvemt.queryCP("CP_C_STORE", "ISWMS", whereJo);
                if (result != null) {
                    String iswms = TypeUtils.castToString(result.getString("ISWMS"));
                    if (!"1".equals(iswms) || mainJo.getString("MODIFY_BILL_NO") == null ) {
                        vh.put("code", 2);
                        vh.put("message", Resources.getMessage("采购入库单不满足 MODIFY_BILL_NO不为空，收货方为WMS管控！"));
                        return vh;
                    }
                }
            }
            mainJo.remove("MODIFY_BILL_NO");
            mainJo.remove("WMS_IN");
            mainJo.put("BILL_TYPE", "PI");
            mainJo.put("RECIVE_STATUS", "80");
            mainJo.put("WRITE_TIME", System.currentTimeMillis());

            fixcolumn.put("mainData", mainJo);
            //查询子表
            List<HashMap> mapList = wmsDlUpdateMapper.selectWmsPurInItem(idStr);
            JSONArray jsonArray = new JSONArray();
            for (int a = 0; a < mapList.size(); a++) {
                HashMap hashMap = (HashMap) mapList.get(a);
                JSONObject jsonObject = new JSONObject(hashMap);
                jsonObject.put("BILL_ID", mainJo.getString("ID"));
                jsonObject.put("BILL_NO", mainJo.getString("BILL_NO"));
                jsonArray.add(jsonObject);
            }
            fixcolumn.put("itemData", jsonArray);
            fixArray.add(fixcolumn);

        }
        vh.put("data", fixArray);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("成功"));
        return vh;

    }

}
